package ${package.Service};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.sql.SqlUtil;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
public class ${table.serviceName}  {
    @Autowired
    private ${table.mapperName} mapper;

    public List<${entity}> get${entity}List(${entity} object){
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();

        // 排序设置，兼容若依的排序参数
        PageDomain pageDomain = TableSupport.buildPageRequest();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        if (StringUtils.isNotBlank(orderBy)) {
            // 解析排序字符串，转换为MyBatis-Plus的排序方式
            if (orderBy.contains("DESC")) {
                String column = orderBy.split(" ")[0];
                queryWrapper.orderByDesc(column);
            } else {
                String column = orderBy.split(" ")[0];
                queryWrapper.orderByAsc(column);
            }
        } else {
            // 默认排序
            queryWrapper.orderByDesc("create_time");
        }
        return mapper.selectList(queryWrapper);
    }

    public ${entity} get${entity}ById(Long id){
        return mapper.selectById(id);
    }

    public int insert${entity}(${entity} object){
        return mapper.insert(object);
    }

    public int update${entity}(${entity} object){
        return mapper.updateById(object);
    }

    public int delete${entity}ByIds(Long[] ids){
        return mapper.deleteBatchIds(Arrays.asList(ids));
    }
}

